package com.sgb.kjwl.viewmodel.self.publish;

import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.user.GoodsEntity;
import com.sgb.kjwl.repository.SelfRepertory;
import com.sgb.kjwl.view.ui.fragment.self.GoodsStateFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MLog;
import com.swgk.core.util.MToast;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class GoodsListModel extends BaseViewModel {
    private GoodsStateFragment fragment;
    private SelfRepertory repertory;

    private int upPage = 1;
    private int downPage = 1;
    private int size = 20;
    private int pageNum = 1;
    public int total;

    public GoodsListModel(GoodsStateFragment fragment, SelfRepertory repertory) {
        this.fragment = fragment;
        this.repertory = repertory;
    }

    /**
     * 获取商品列表
     *
     * @param status
     */
    public void getGoodsList(String status, boolean loadMore) {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "获取信息中...", 0, false, null).setCanceledOnTouchOutside(false);
        if (status.equals("1")) {
            upPage = loadMore ? ++upPage : 1;
            pageNum = upPage;
        } else {
            downPage = loadMore ? ++downPage : 1;
            pageNum = downPage;
        }
        APIManager.getInstance().getGoodsAPI().getGoodsList(status, pageNum, size).enqueue(new Callback<BaseEntity<GoodsEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<GoodsEntity>> call, Response<BaseEntity<GoodsEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    total = response.body().getData().getTotalRow();
                    fragment.showGoodsList(response.body().getData().getList(), loadMore);
                } else {
                    MToast.showToast(fragment.getActivity(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<GoodsEntity>> call, Throwable throwable) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(fragment.getActivity(), throwable.getMessage());
            }
        });
    }

    /**
     * 上架商品
     *
     * @param id
     */
    public void upGoods(String id, int position) {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "上架中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getGoodsAPI().upGoods(id).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.refreshList(position);
                } else {
                    MToast.showToast(fragment.getActivity(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable throwable) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(fragment.getActivity(), throwable.getMessage());
            }
        });
    }

    /**
     * 下架商品
     *
     * @param id
     */
    public void downGoods(String id, int position) {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "下架中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getGoodsAPI().downGoods(id).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.refreshList(position);
                } else {
                    MToast.showToast(fragment.getActivity(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable throwable) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(fragment.getActivity(), throwable.getMessage());
            }
        });
    }

    /**
     * 删除商品
     *
     * @param id
     */
    public void deleteGoods(String id, int position) {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "删除中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getGoodsAPI().deleteGoods(id).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    MToast.showToast(fragment.getActivity(), "删除成功");
                    fragment.refreshList(position);
                } else {
                    MToast.showToast(fragment.getActivity(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable throwable) {
                DialogHelper.dismissProgressDialog();
                MLog.d(AppConstant.API_LOG, throwable.getMessage());
            }
        });
    }

    public void modifyPrice(String id, String price, String type) {
        APIManager.getInstance().getGoodsAPI().modifyPrice(id, price).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                if (response.body() == null || !response.body().getState().equals("ok")) {
                    MToast.showToast(fragment.getActivity(), "修改失败");
                } else {
                    MToast.showToast(fragment.getActivity(), "修改成功");
                    fragment.initData();
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {

            }
        });
    }
}
